﻿
Partial Class Paginas_MantencionLugares
    Inherits System.Web.UI.Page

#Region "Declaracion de Funciones y Procedimientos de pagina"
    Private Sub CargarInfoConvenio()
        Dim objNegConv As New dllNegocio.NegConvenios(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Dim myCollect As Collection
        Dim objConv As New dllEntidades.clsConvenio
        Try
            myCollect = objNegConv.ListarConvenios(Me.hdnIdConvenio.Value.Trim, "TODOS", 0)
            If myCollect.Count > 0 Then
                objConv = myCollect.Item(1)
                Me.lblConvenio.Text = objConv.Nombre
                Me.lblFecHoraIni.Text = objConv.FechaHoraInicio.ToString
                Me.lblFecHoraFin.Text = objConv.FechaHoraFinal.ToString
            End If
            Call CargarGrillaLugaresConvenio()
            Call CargarComboLugaresFaltantes()
        Catch ex As Exception
        Finally
            objNegConv.Dispose()
            objNegConv = Nothing
            myCollect = Nothing
            objConv.Dispose()
            objConv = Nothing
        End Try
    End Sub

    Private Sub CargarGrillaLugaresConvenio()
        Dim objNegConvLug As New dllNegocio.NegLugaresConvenios(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Dim myCollect As Collection

        Try
            myCollect = objNegConvLug.ListarLugaresDeConvenio(Me.hdnIdConvenio.Value.Trim)
            Me.GrillaResultados.DataSource = myCollect
            Me.GrillaResultados.DataBind()
        Catch ex As Exception
        Finally
            myCollect = Nothing
            objNegConvLug.Dispose()
            objNegConvLug = Nothing
        End Try
    End Sub

    Private Sub CargarComboLugaresFaltantes()
        Dim objNegConvLug As New dllNegocio.NegLugaresConvenios(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Dim myCollect As Collection

        Try
            myCollect = objNegConvLug.ListarLugaresFaltantes(Me.hdnIdConvenio.Value.Trim)
            Me.ddlLugares.DataSource = myCollect
            Me.ddlLugares.DataTextField = "Descripcion"
            Me.ddlLugares.DataValueField = "Codigo"
            Me.ddlLugares.DataBind()
        Catch ex As Exception
        Finally
            myCollect = Nothing
            objNegConvLug.Dispose()
            objNegConvLug = Nothing
        End Try

    End Sub

    Public Function CrearBtnEliminar(ByVal Pk_IdLugarConvenio As String) As String
        Dim strHTML As String = ""
        strHTML = "<img src='img/eliminar.gif' style='cursor:pointer;' onClick='EliminarConvenioLugar(" & Pk_IdLugarConvenio.Trim & ");'/>"
        Return strHTML
    End Function

    Private Sub EliminarConvenioLugar(ByVal IdConvenioLugar As String)
        Dim objNeg As New dllNegocio.NegLugaresConvenios(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Dim objConvenioLugar As New dllEntidades.clsLugarConvenio
        Try
            With objConvenioLugar
                .Codigo = IdConvenioLugar
            End With
            Dim ResulGrabar As Boolean

            ResulGrabar = objNeg.EliminarLugarConvenio(objConvenioLugar)

            If ResulGrabar = True Then
                UtilidadesPagina.mensaje("el lugar ah sido eliminado correctamente", Me.Page)
                Call CargarInfoConvenio()
            End If
        Catch ex As Exception
        Finally
            objNeg.Dispose()
            objConvenioLugar.Dispose()
            objNeg = Nothing
            objConvenioLugar = Nothing
        End Try
    End Sub

    Private Sub GuardarConvenioLugar()
        Dim objNeg As New dllNegocio.NegLugaresConvenios(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Dim objConvenioLugar As New dllEntidades.clsLugarConvenio
        Try
            With objConvenioLugar
                .CodigoConvenio = Me.hdnIdConvenio.Value.Trim
                .CodigoLugar = Me.ddlLugares.SelectedValue.Trim
                .PrecioAdulto = Me.txtPrecioAdulto.Text.Trim
                .PrecioInfantil = Me.txtPrecioInfantil.Text.Trim
                .PrecioMayores = Me.txtPrecioTerceraEdad.Text.Trim
            End With
            Dim ResulGrabar As Boolean

            ResulGrabar = objNeg.GrabarLugarConvenio(objConvenioLugar)

            If ResulGrabar = True Then
                UtilidadesPagina.mensaje("el lugar ah sido guardado correctamente", Me.Page)
                Call CargarInfoConvenio()
            End If
        Catch ex As Exception
        Finally
            objNeg.Dispose()
            objConvenioLugar.Dispose()
            objNeg = Nothing
            objConvenioLugar = Nothing
        End Try
    End Sub
#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Me.hdnIdConvenio.Value = Request.QueryString("ConvenioID")
            Call CargarInfoConvenio()
        Else
            Dim PrimerArgumento As String = Request.Params.Get("__EVENTARGUMENT")
            If PrimerArgumento = "true" Then
                Try
                    Dim SegundoArgumento As String = Request.Params.Get("__EVENTTARGET").Split("|")(1)
                    If IsNumeric(SegundoArgumento) Then
                        Call EliminarConvenioLugar(SegundoArgumento)
                    End If
                Catch ex As Exception
                End Try
            End If
        End If
    End Sub

    Protected Sub btnVolver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVolver.Click
        Response.Redirect("MantenerConvenios.aspx?ConvenioID=" & Me.hdnIdConvenio.Value.Trim)
        Response.End()
    End Sub

    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        Call GuardarConvenioLugar()
    End Sub
End Class
